package com.example.coinhelper.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.coinhelper.Bean.Details;
import com.example.coinhelper.Bean.Note;
import com.example.coinhelper.R;

import java.util.List;

public class NoteAdapter extends BaseAdapter {
    private Context context;
    private List<Note> noteList;

    public NoteAdapter(Context context, List<Note> noteList) {
        this.context = context;
        this.noteList = noteList;
    }
    public void setNoteList(List<Note> noteList){
        this.noteList = noteList;
    }
    @Override
    public int getCount() {
        return noteList.size();
    }

    @Override
    public Object getItem(int position) {
        return noteList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Inflate your list item layout
        View view = View.inflate(context, R.layout.item_note, null);

        // Get references to the views in the layout for each note item
        TextView titleTextView = view.findViewById(R.id.text_view_title);
        TextView firstSentenceTextView = view.findViewById(R.id.text_view_first_sentence);
        TextView timeTextView = view.findViewById(R.id.text_view_time);

        // Get the Note object for this position
        Note note = noteList.get(position);

        // Set data to the views
        titleTextView.setText(note.getTitle());
        String bodyText = note.getBody();
        if (bodyText != null && bodyText.length() >= 16) {
            firstSentenceTextView.setText(bodyText.substring(0, 16));
        } else if (bodyText != null) {
            firstSentenceTextView.setText(bodyText); // 如果长度不足16，则设置整个字符串作为文本
        } else {
            firstSentenceTextView.setText(""); // 或者设置一个默认的空字符串
        }
        timeTextView.setText(note.getDate());

        return view;
    }
}
